package com.service.Impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.ArticleMapper;
import com.mapper.CCommentMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CArticle;
import com.pojo.entity.CComment;
import com.pojo.vo.CommentVo;
import com.service.CommentService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("CommentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    CCommentMapper cCommentMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    Snowflake snowflake;

    @Override
    public Message<List<CommentVo>> list(CommentVo commentVo) {
        QueryWrapper<CComment> wrapper = new QueryWrapper<>();
        if (commentVo != null) {
            if (commentVo.getArticleId() != null) wrapper.eq("article_id", commentVo.getArticleId());
            if (commentVo.getUserNum() != null) wrapper.eq("user_num", commentVo.getUserNum());
            if (commentVo.getCommentId() != null) wrapper.eq("comment_id", commentVo.getCommentId());
            if (commentVo.getCommentType() != null) wrapper.eq("comment_type", commentVo.getCommentType());
            if (commentVo.getPublishTime() != null) wrapper.eq("publish_time", commentVo.getPublishTime());
        }
        List<CComment> cComments = cCommentMapper.selectList(wrapper);
        return Message.successMessage((long) cComments.size(), PojoUtil.copyList(cComments, CommentVo.class));
    }

    @Override
    public Message<CommentVo> get(CommentVo commentVo) {
        QueryWrapper<CComment> wrapper = new QueryWrapper<>();
        wrapper.eq("id", commentVo.getId());
        CComment comment = cCommentMapper.selectOne(wrapper);
        return Message.successMessage(1L, PojoUtil.copyObject(comment, CommentVo.class));
    }

    @Override
    public Message<CommentVo> edit(CommentVo commentVo) {
        CComment comment = PojoUtil.copyObject(commentVo, CComment.class);
        int i = cCommentMapper.updateById(comment);//根据ID更新book表
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, commentVo);
    }

    @Override
    public Message<CommentVo> add(CommentVo commentVo) {
        CComment comment = PojoUtil.copyObject(commentVo, CComment.class);
        comment.setCommentId(snowflake.nextId());
        int i = cCommentMapper.insert(comment);
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        QueryWrapper<CComment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", commentVo.getArticleId());
        Integer integer = cCommentMapper.selectCount(wrapper);
        CArticle cArticle = articleMapper.selectById(commentVo.getArticleId());
        cArticle.setCommentCount(Long.valueOf(integer + 1));
        int a = articleMapper.updateById(cArticle);
        if (a == 0) return Message.errorMessage("1001", "添加失败");
        else return Message.successMessage(1L, commentVo);
    }

    @Override
    public Message<CommentVo> delete(CommentVo commentVo) {
        int i = cCommentMapper.deleteById(commentVo.getId());
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        QueryWrapper<CComment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", commentVo.getArticleId());
        Integer integer = cCommentMapper.selectCount(wrapper);
        CArticle cArticle = articleMapper.selectById(commentVo.getArticleId());
        cArticle.setCommentCount(Long.valueOf(integer - 1));
        int a = articleMapper.updateById(cArticle);
        if (a == 0) return Message.errorMessage("1001", "添加失败");

        else return Message.successMessage(1L, null);
    }
}
